package com.pan.admin.module.visualizer.controller;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.Empty;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.IteratorUtils;
import com.gitee.apanlh.util.base.enums.Sort;
import com.gitee.apanlh.util.date.DateUtils;
import com.gitee.apanlh.util.date.format.DateTimeFormat;
import com.gitee.apanlh.util.valid.ValidParam;
import com.pan.admin.common.constant.ProductApplyFlowStatusEnum;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.module.visualizer.entity.po.ApiVisualizerPO;
import com.pan.admin.module.visualizer.entity.qo.ApiVisualizerQO;
import com.pan.admin.module.visualizer.entity.vo.ApiVisualizerVO;
import com.pan.admin.module.visualizer.entity.vo.ReqApiVisualizerVO;
import com.pan.admin.module.visualizer.service.ApiVisualizerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 *  API数据统计
 *  @author Pan
 */
@RestController
@RequestMapping("/visualizer/api")
public class ApiVisualizerController extends BaseController {

    @Autowired
    private ApiVisualizerService apiVisualizerService;

    /**
     *  获取API渠道统计数据
     *
     *  @return TableDataInfo
     */
    @PreAuthorize("@ss.hasPermi('visualizer:api:query')")
    @GetMapping("/list")
    public TableDataInfo list(ReqApiVisualizerVO reqApiVisualizerVO) {
        Map<String, Object> params = reqApiVisualizerVO.getParams();
        String startTime = String.valueOf(params.get("beginTime"));
        String endTime = String.valueOf(params.get("endTime"));

        boolean hasNow = false;
        if (ValidParam.isEmpty(startTime) && ValidParam.isEmpty(endTime)) {
            String date = DateUtils.currentTime(DateTimeFormat.DATE_FULL);
            startTime = date + " 00:00:00";
            endTime = DateUtils.currentTime();
            hasNow = true;
        } else {
            startTime = startTime + " 00:00:00";
            endTime = endTime + " 00:00:00";
        }

        String displayDateTime;
        //  拼接时间
        if (hasNow) {
            displayDateTime = "当日数据截止至" + endTime;
        } else {
            displayDateTime = startTime + "至" + endTime;
        }

        //  获取启用的API渠道信息
        ApiVisualizerQO api = new ApiVisualizerQO();
        api.setStartTime(startTime);
        api.setEndTime(endTime);
        List<ApiVisualizerPO> listApiKey = apiVisualizerService.listApiClientId(api);
        if (ValidParam.isEmpty(listApiKey)) {
            return getDataTable(Empty.list());
        }

        //  获取渠道账单数据
        ApiVisualizerQO billQO = new ApiVisualizerQO();
        billQO.setStartTime(startTime);
        billQO.setEndTime(endTime);
        billQO.setListApiClientId(CollUtils.toList(listApiKey, t -> t.getApiClientId()));
        List<ApiVisualizerPO> listSumBill = apiVisualizerService.listBill(billQO);

        //  分组获取账单数据
        List<ApiVisualizerPO> listGroup = CollUtils.newArrayList();
        for (ApiVisualizerPO apikey : listApiKey) {
            String apiClientId = apikey.getApiClientId();

            //  获取基础数据
            ApiVisualizerQO apiVisualizerQO = new ApiVisualizerQO();
            apiVisualizerQO.setStartTime(startTime);
            apiVisualizerQO.setEndTime(endTime);
            apiVisualizerQO.setApiClientId(apiClientId);

            //  获取渠道流程数据
            List<ApiVisualizerPO> listApiVisualizerPO = apiVisualizerService.listFlow(apiVisualizerQO);
            IteratorUtils.array(listApiVisualizerPO, t -> listGroup.add(t));

            //  设置成本及收入
            ApiVisualizerPO sumBill = CollUtils.findOne(listSumBill, t -> Eq.str(apiClientId, t.getApiClientId()));
            ApiVisualizerPO sucFlow = CollUtils.findOne(listApiVisualizerPO, t -> Eq.object(t.getStatus(), ProductApplyFlowStatusEnum.USER_ACCEPT.getType()));
            if (ValidParam.isNotNull(sucFlow)) {
                sucFlow.setTotalRevenue(sumBill.getTotalRevenue());
                sucFlow.setTotalCost(apikey.getTotalCost());
            }
        }

        //  转换VO实体
        List<ApiVisualizerVO> convert = ApiVisualizerVO.convert(listGroup);

        convert.forEach(apiVisualizerVO -> apiVisualizerVO.setDateTime(displayDateTime));
        //  排序名称
        CollUtils.sortStr(convert, "apiName", Sort.DESC);
        return getDataTable(convert);
    }
}
